<template>
  <div class="app-container">
    <el-card shadow="never">
      <el-form label-position="right" label-width="150px" ref="form" :model="form">
        <el-form-item label="保险费用">
          <el-input v-model.trim="form.insureCost" style="width: 300px;"></el-input>
          元
        </el-form-item>
        <el-form-item label="">
          <el-button @click="submit(row)" type="primary">保存</el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import * as premium from '../../api/premium'
export default {
  data () {
    return {
      form: {},
      status: false
    }
  },
  created () {
    this.getList()
  },
  methods: {
    getList () {
      this.listLoading = true
      const param = Object.assign({}, this.searchForm)
      premium.queryByPage(param).then(response => {
        let list = response.data.rows || []
        if (list.length == 0) {
          this.status = true
        } else {
          this.form = Object.assign({}, list[0])
        }
      }).catch(error => {
        console.log(error)
      })
    },
    submit () {
      if (this.status) {
        this.creatHandle()
      } else {
        this.editHandle()
      }
    },
    creatHandle () {
      premium.insert(this.form).then(response => {
        if (response.code == 200) {
          this.getList()
          this.$message.success('新增成功')
        }
      }).catch(error => {
        console.log(error)
      })
    },
    editHandle () {
      delete this.form.createTime
      delete this.form.updateTime
      premium.update(this.form).then(response => {
        if (response.code == 200) {
          this.getList()
          this.$message.success('修改成功')
        }
      }).catch(error => {
        console.log(error)
      })
    },
    delHandle (data) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const param = {
          uid: data.uid,
        }
        premium.deleteById(param).then(result => {
          this.$message.success(result.msg)
          this.getList()
        }).catch(error => {
          this.loading = false
          console.log(error)
        })
      }).catch(() => {
        console.log('取消删除')
      });
    }
  }
}
</script>
